<%@page import="net.groupme.user.UserManager"%>
<%@page import="java.util.List"%>
<%@page import="net.groupme.model.Group"%>
<%@page import="java.util.Iterator"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="net.groupme.gui.constants.VIEW_VOCABULARY"%>
<%@page import="java.util.Collections"%>
<%@page import="net.groupme.model.dao.DATABASE_CONSTANTS"%>
<%@page import="java.util.Collection"%>
<%@page import="net.groupme.model.Tag"%>
<div class="grouplistHead">
	<div class="groupListHeadMenu">
	<table>
		<tr>
			<td>sort by: </td>
			<td>

		<form>
			<select onchange="location.href=this.options[this.selectedIndex].value;">
				<option>please select</option>
				<option value="./<%=VIEW_VOCABULARY.ACTION_HOME + "?" 
						+ VIEW_VOCABULARY.PARAMETER_MYHOME_VIEW + "=" + VIEW_VOCABULARY.VIEW_MYGROUPS + "&" 
						+ VIEW_VOCABULARY.PARAMETER_SORT + "=" + VIEW_VOCABULARY.SORT_ALPHA_ASC %>">alphabet (a..z)</option>
				<option value="./<%=VIEW_VOCABULARY.ACTION_HOME + "?" 
						+ VIEW_VOCABULARY.PARAMETER_MYHOME_VIEW + "=" + VIEW_VOCABULARY.VIEW_MYGROUPS + "&" 
						+ VIEW_VOCABULARY.PARAMETER_SORT + "=" + VIEW_VOCABULARY.SORT_ALPHA_DESC %>">alphabet (z..a)</option>
				<option value="./<%=VIEW_VOCABULARY.ACTION_HOME + "?" 
						+ VIEW_VOCABULARY.PARAMETER_MYHOME_VIEW + "=" + VIEW_VOCABULARY.VIEW_MYGROUPS + "&" 
						+ VIEW_VOCABULARY.PARAMETER_SORT + "=" + VIEW_VOCABULARY.SORT_TAD_DESC %>">date (newest first)</option>
				<option value="./<%=VIEW_VOCABULARY.ACTION_HOME + "?" 
						+ VIEW_VOCABULARY.PARAMETER_MYHOME_VIEW + "=" + VIEW_VOCABULARY.VIEW_MYGROUPS + "&" 
						+ VIEW_VOCABULARY.PARAMETER_SORT + "=" + VIEW_VOCABULARY.SORT_TAD_ASC %>">date (oldest first)</option>
			</select>
		</form>
		</td>
		</tr>
	</table>
	</div>
	<h3>Your Groups 
		<span style="font-size: x-small; color: gray; ">
		<% 	String orderedBy = "";
		List<Group> mygroups = null;
		UserManager um = (UserManager) WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext())
		.getBean(VIEW_VOCABULARY.USER_MANGER, UserManager.class);
		if (VIEW_VOCABULARY.SORT_ALPHA_ASC.equals(request.getParameter(VIEW_VOCABULARY.PARAMETER_SORT))){
			mygroups = um.getAllGroupsOfUserOrderedBy(DATABASE_CONSTANTS.RESOURCE_NAME);
			orderedBy = "in alphabetical order (a..z)";
		}
		else if (VIEW_VOCABULARY.SORT_ALPHA_DESC.equals(request.getParameter(VIEW_VOCABULARY.PARAMETER_SORT))){
			mygroups = um.getAllGroupsOfUserOrderedBy(DATABASE_CONSTANTS.RESOURCE_NAME);
			Collections.reverse(mygroups);
			orderedBy = "in alphabetical order (z..a)";
		}
		else if (VIEW_VOCABULARY.SORT_TAD_ASC.equals(request.getParameter(VIEW_VOCABULARY.PARAMETER_SORT))){
			mygroups = um.getAllGroupsOfUserOrderedBy(DATABASE_CONSTANTS.TABLE_USERGROUP + "." + DATABASE_CONSTANTS.USER_GROUP_TAD);
			orderedBy = "in temporal order (oldest first)";
		}
		else {
			mygroups = um.getAllGroupsOfUserOrderedBy(DATABASE_CONSTANTS.TABLE_USERGROUP + "." + DATABASE_CONSTANTS.USER_GROUP_TAD);
			Collections.reverse(mygroups);
			orderedBy = "in temporal order (newest first)";
		}
			%>
			<%= orderedBy %>
		</span> 
	</h3>
</div>
<div id="mainGroupwindowContent">

		<%
	Iterator it = mygroups.iterator(); %>
	<%@include file="list-groups.jsp"%>	
		
</div>
